package com.cn.sys.main.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.cn.sys.frame.dao.BaseSpringJdbcDao;
import com.cn.sys.frame.exception.ServiceException;
import com.cn.sys.frame.service.impl.BaseServiceImpl;
import com.cn.sys.frame.util.page.PageData;
import com.cn.sys.frame.util.query.QueryBean;
import com.cn.sys.frame.util.query.QueryCondition;
import com.cn.sys.main.annotation.OperationDescription;
import com.cn.sys.main.dao.SysFunctionDao;
import com.cn.sys.main.dao.SysModuleDao;
import com.cn.sys.main.dao.SysRoleModuleDao;
import com.cn.sys.main.entity.SysModule;
import com.cn.sys.main.service.SysModuleService;
import com.cn.sys.main.util.MenuUtil;

@Service("sysModuleService")
public class SysModuleServiceImpl extends BaseServiceImpl<SysModule, Long> implements
		SysModuleService {

	private SysModuleDao sysModuleDao;
	@Autowired
	private SysRoleModuleDao sysRoleModuleDao;
	@Autowired
	private SysFunctionDao sysFunctionDao;
	
	@Autowired
	public void setBaseDao(BaseSpringJdbcDao<SysModule, Long> sysModuleDao) {
		this.baseDao = sysModuleDao;
		this.sysModuleDao = (SysModuleDao) sysModuleDao;
	}

	@Cacheable(value="sysModuleCache", key=" #id + 'get'")
	public SysModule get(Long id) throws Exception {
		return super.get(id);
	}

	@OperationDescription(operateType = "save", 
			content = "添加模块\nID:${#entity.id}\n模块名称:${#entity.moduleName}\n" +
					"请求地址:${#entity.url}\n子节点:${#entity.isLeaf}\n父模块:${#entity.parentId}", 
			operateObj = "SysModule")
	public SysModule save(SysModule entity) throws Exception {
		return super.save(entity);
	}

	@OperationDescription(operateType = "update", 
			content = "修改模块\nID:${#entity.id}\n模块名称:${#entity.moduleName}\n" +
					"请求地址:${#entity.url}\n子节点:${#entity.isLeaf}\n父模块:${#entity.parentId}", 
			operateObj = "SysModule")
	@CacheEvict(value="sysModuleCache", key=" #entity.id + 'get'")
	public SysModule update(SysModule entity) throws Exception {
		return super.update(entity);
	}

	@OperationDescription(operateType = "delete", content = "删除模块\nID:${#id}", operateObj = "SysModule")
	@CacheEvict(value="sysModuleCache", key=" #id + 'SysModuleServiceImpl'")
	@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class, readOnly=false)
	public int delete(Long id) throws Exception {
		QueryCondition queryCondition = new QueryCondition();
		QueryBean qb = new QueryBean(" WHERE MODULE_ID = ?", "MODULE_ID", id);
		queryCondition.setQueryBean(qb);
		int result = 0;
		try {
			sysRoleModuleDao.delete(queryCondition);
			sysFunctionDao.delete(queryCondition);
			result = sysModuleDao.delete(id);
		} catch (Exception e) {
			//e.printStackTrace();
			throw new ServiceException("删除模块出错! ["+e.getMessage()+"]");
		}
		return result;
	}

	@Override
	public long getCount(QueryCondition queryCondition) throws Exception {
		return super.getCount(queryCondition);
	}

	@Override
	public PageData<SysModule> getPageData(PageData<SysModule> pageData,
			QueryCondition queryCondition) throws Exception {
		return super.getPageData(pageData, queryCondition);
	}

	public List<SysModule> getAvailableModules() throws Exception{
		QueryCondition queryCondition = new QueryCondition();
		QueryBean qb = new QueryBean("AND STATUS = ?", "STATUS", "1");
		queryCondition.setQueryBean(qb);
		List<SysModule> list = sysModuleDao.listByCondition(queryCondition);
		list = MenuUtil.getFormatModule(list);
		//sysModuleDao.listByCondition(" AND STATUS = ? START WITH PARENT_ID IS NULL  CONNECT BY PRIOR ID = PARENT_ID ORDER SIBLINGS BY ORDER_INDEX ASC", "1");
		return list;
	}

	public List<SysModule> getAvailableModulesByUserId(Long userId) throws Exception{
		String sql = "SELECT DISTINCT T3.ID ,T3.MODULE_NAME ,T3.URL ,T3.IS_LEAF ,T3.ORDER_INDEX ,T3.STATUS ,T3.REMARK ,T3.PARENT_ID ,T3.ICON " +
				"FROM SYS_USER_ROLE T1,SYS_ROLE_MODULE T2,SYS_MODULE T3 WHERE T1.ROLE_ID=T2.ROLE_ID AND T2.MODULE_ID=T3.ID AND T3.STATUS= ? AND T1.USER_ID= ?";
		List<SysModule> list = sysModuleDao.listBySql(sql, "1", userId);
		list = MenuUtil.getFormatModule(list);
		return list;
	}

	public List<Map<String, Object>> getAllList() throws Exception{
		String sql = "SELECT T.ID \"ID\",T.MODULE_NAME \"MODULE_NAME\",T.URL \"URL\",T.ICON \"ICON\",T.IS_LEAF \"IS_LEAF\",T.ORDER_INDEX \"ORDER_INDEX\",T.STATUS \"STATUS\"," +
				"T.PARENT_ID \"_parentId\" FROM SYS_MODULE T ";
		List<Map<String, Object>> list = sysModuleDao.getJdbcTemplate().queryForList(sql);
		return list;
	}

	public List<SysModule> getAll() throws Exception{
		return MenuUtil.getFormatModule(sysModuleDao.listAll());
	}

	public List<SysModule> getParentModules(String id) throws Exception{
		QueryCondition queryCondition = null;
		if(StringUtils.isNotBlank(id)){
			queryCondition = new QueryCondition();
			QueryBean qb = new QueryBean(" AND ID <> ?", "ID", Long.valueOf(id));
			queryCondition.setQueryBean(qb);
		}
		List<SysModule> list = sysModuleDao.listByCondition(queryCondition);
		return list;
	}
	
}
